import { PlayerEntity } from '../domain/entity/PlayerEntity';
import { PlayerAchievementValue } from '../domain/value/PlayerAchievementValue';
import { PlayerDataSource, MockPlayerDataSource } from '../data/datasource/PlayerDataSource';

// 玩家仓库接口
export interface PlayerRepository {
  // 玩家相关方法
  getAllPlayers(): Promise<PlayerEntity[]>;
  getPlayerById(id: string): Promise<PlayerEntity | null>;
  getPlayerByUsername(username: string): Promise<PlayerEntity | null>;
  savePlayer(player: PlayerEntity): Promise<boolean>;
  updatePlayer(player: PlayerEntity): Promise<boolean>;
  deletePlayer(id: string): Promise<boolean>;
  
  // 成就相关方法
  getPlayerAchievements(playerId: string): Promise<PlayerAchievementValue[]>;
  addPlayerAchievement(achievement: PlayerAchievementValue): Promise<boolean>;
  savePlayerAchievements(playerId: string, achievements: PlayerAchievementValue[]): Promise<boolean>;
  
  // 批量操作
  getMultiplePlayers(ids: string[]): Promise<PlayerEntity[]>;
}

// 玩家仓库实现
export class PlayerRepositoryImpl implements PlayerRepository {
  private dataSource: PlayerDataSource;
  
  constructor() {
    // #ifdef PREVIEW
    // 预览模式使用Mock数据源
    this.dataSource = new MockPlayerDataSource();
    // #else
    // 真实环境使用JSON文件数据源（后续实现）
    /*
    this.dataSource = new PlayerJsonDataSource();
    */
    // #endif
  }
  
  async getAllPlayers(): Promise<PlayerEntity[]> {
    try {
      return await this.dataSource.getAllPlayers();
    } catch (error) {
      console.error('Failed to get all players:', error);
      return [];
    }
  }
  
  async getPlayerById(id: string): Promise<PlayerEntity | null> {
    try {
      return await this.dataSource.getPlayerById(id);
    } catch (error) {
      console.error(`Failed to get player by id ${id}:`, error);
      return null;
    }
  }
  
  async getPlayerByUsername(username: string): Promise<PlayerEntity | null> {
    try {
      return await this.dataSource.getPlayerByUsername(username);
    } catch (error) {
      console.error(`Failed to get player by username ${username}:`, error);
      return null;
    }
  }
  
  async savePlayer(player: PlayerEntity): Promise<boolean> {
    try {
      return await this.dataSource.savePlayer(player);
    } catch (error) {
      console.error('Failed to save player:', error);
      return false;
    }
  }
  
  async updatePlayer(player: PlayerEntity): Promise<boolean> {
    try {
      return await this.dataSource.updatePlayer(player);
    } catch (error) {
      console.error(`Failed to update player ${player.id}:`, error);
      return false;
    }
  }
  
  async deletePlayer(id: string): Promise<boolean> {
    try {
      return await this.dataSource.deletePlayer(id);
    } catch (error) {
      console.error(`Failed to delete player ${id}:`, error);
      return false;
    }
  }
  
  async getPlayerAchievements(playerId: string): Promise<PlayerAchievementValue[]> {
    try {
      return await this.dataSource.getPlayerAchievements(playerId);
    } catch (error) {
      console.error(`Failed to get achievements for player ${playerId}:`, error);
      return [];
    }
  }
  
  async addPlayerAchievement(achievement: PlayerAchievementValue): Promise<boolean> {
    try {
      return await this.dataSource.addPlayerAchievement(achievement);
    } catch (error) {
      console.error('Failed to add achievement:', error);
      return false;
    }
  }
  
  async savePlayerAchievements(playerId: string, achievements: PlayerAchievementValue[]): Promise<boolean> {
    try {
      return await this.dataSource.savePlayerAchievements(playerId, achievements);
    } catch (error) {
      console.error(`Failed to save achievements for player ${playerId}:`, error);
      return false;
    }
  }
  
  async getMultiplePlayers(ids: string[]): Promise<PlayerEntity[]> {
    try {
      const allPlayers = await this.getAllPlayers();
      return allPlayers.filter(player => ids.includes(player.id));
    } catch (error) {
      console.error('Failed to get multiple players:', error);
      return [];
    }
  }
}
